// JavaScript Document
function updateSelectsAdresa(obj,type){
	var i;
	var id = obj.options[obj.selectedIndex].getAttribute("value");
	var parent = getParent(obj);
	var selects = getSelects(obj);
	var spans = getSpans(obj);
	var ptt,tmpPtt, pttObj;
	for(i=0; i<spans.length; i++){
		if(spans[i].className == "ptt"){
			pttObj = spans[i];
			break;
		}
	}
	ptt = trim(obj.options[obj.selectedIndex].getAttribute("ptt"));
	
	var index;
	switch(type){
		case "region":
			index=0;
			break;
		case "opstina":
			index=1;
			break;
		case "grad":
			index=2;
			break;
		case "lok_opstina":
			index=3;
			break;
		case "naselje":
			index=4;
			break;
	}
	//window.status = id;
	var prev, opts, selected;
	// UNAPRED
	for(i=(index+1); i<selects.length; i++ ){
		prev = selects[i-1].value;
		opts = selects[i].options;
		opts[0].selected = true;
		for(var j=0; j<opts.length; j++){
			if(opts[j].value<1 || opts[j].getAttribute("parent")==prev || prev<1){
				opts[j].style.display = "block";
			}else{
				opts[j].style.display = "none";
			}
		}
	}
	// UNAZAD
	for(i=index; i>0; i--){
		var obj = selects[i];
		prev = selects[i-1];
		opts = prev.options;
		for(j=0; j<opts.length; j++){
			opts[j].style.display = "block";
			if(opts[j].value == obj.options[obj.selectedIndex].getAttribute("parent")){
				opts[j].selected = true;
				if(!ptt.length){
					var tmpPtt = trim(opts[j].getAttribute("ptt"));
					if(tmpPtt.length){
						ptt = tmpPtt
					}
				}
			}
		}
	}
	if(pttObj){
		pttObj.innerHTML = ptt;
	}
	////////////////////////
	//		functions	  //
	////////////////////////
	
	function getParent(o){
		return o.options[o.selectedIndex].getAttribute("parent");
	}
	function getSelects(o){
		var selects = o.parentNode.getElementsByTagName("select");
		return selects;
	}
	function getSpans(o){
		return o.parentNode.getElementsByTagName("span");
	}
}
function updateLokacijaSelects(sel,type,objects){
	var i;
	if(type==1){	// GRAD ON CHANGE
		var parents = Array();
		var obj=document.getElementById(objects[0]);
		for(i=0; i<obj.options.length; i++){
			if((obj.options[i].getAttribute("parent")==sel.value && sel.value>0) || sel.value<0){
				parents.push(obj.options[i].value);
				obj.options[i].style.display='block';
				obj.options[i].disabled=false;
			}else if(obj.options[i].value>0){
				obj.options[i].style.display='none';
				obj.options[i].disabled=true;
			}
		}
		obj.selectedIndex=0;
		
		var obj = document.getElementById(objects[1]);
		for(i=0; i<obj.options.length; i++){
			var par = obj.options[i].getAttribute("parent");
			var ima = parents.indexOf(par);
			if((ima>=0) || obj.options[i].value<0){
				obj.options[i].style.display="block";
				obj.options[i].disabled=false;
			}else{
				obj.options[i].style.display="none";
				obj.options[i].disabled=true;
			}
		}
		obj.selectedIndex=0;
		
	}else if(type==2){	// LOK.OPSTINA ON CHANGE
		var obj=document.getElementById(objects[0]);
		var par = sel.options[sel.selectedIndex].getAttribute("parent")
		if(par>0){
			for(i=0; i<obj.options.length; i++){
				if(obj.options[i].value==par){
					obj.selectedIndex = i;
					break;
				}
			}
		}
		
		var obj = document.getElementById(objects[1]);
		for(i=0; i<obj.options.length; i++){
			var par = obj.options[i].getAttribute("parent");
			if((par==sel.value) || obj.options[i].value<0){
				obj.options[i].style.display="block";
				obj.options[i].disabled=false;
			}else{
				obj.options[i].style.display="none";
				obj.options[i].disabled=true;
			}
		}
		obj.selectedIndex=0;
		
	}else if(type==3){	// NASELJE ON CHANGE
		var obj=document.getElementById(objects[1]);
		var par = sel.options[sel.selectedIndex].getAttribute("parent")
		if(par>0){
			for(i=0; i<obj.options.length; i++){
				if(obj.options[i].value==par){
					par = obj.options[i].getAttribute("parent");
					obj.selectedIndex = i;
					break;
				}
			}
			var obj = document.getElementById(objects[0]);
			for(i=0; i<obj.options.length; i++){
				if(obj.options[i].value==par){
					obj.selectedIndex=i;
					break;
				}
			}
		}
	}
}